//
//  Token.swift
//  Employee
//
//  Created by swordjoy on 2017/4/1.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation

let Token_Key = "Token_Key"
let RefreshToken_Key = "RefreshToken_Key"

/// 记录token
struct Token {
  
  static let shared = Token()
  
    private init() {
        
    }
    
  var token: String {
    return (Token.tokenInfoDic?.object(forKey: Token_Key) as? String) ?? ""
  }
  
  var refreshToken: String {
    return (Token.tokenInfoDic?.object(forKey: RefreshToken_Key) as? String) ?? ""
  }
  
  func cacheToken(token: String, refreshToken: String) {
    
    let tokenDic = NSMutableDictionary()
    
    tokenDic.setObject(token, forKey: Token_Key as NSString)
    tokenDic.setObject(refreshToken, forKey: RefreshToken_Key as NSString)
    
    tokenDic.write(to: Token.cacheFileName, atomically: true)
  }
  
  static var cacheFileName: URL {
    
    return SJApp.cachesURL.appendingPathComponent("Token.plist")
    
  }
  
  static var tokenInfoDic: NSMutableDictionary? {
    
    return NSMutableDictionary(contentsOf: Token.cacheFileName)
    
  }
  
  static func clearAll() {
    
    let clearDic = NSDictionary()
    clearDic.write(to: Token.cacheFileName, atomically: true)
    
  }
  
}
